package blue.mqtt4j.core.lexer;

import java.util.List;

/**
 * @author Jin Zheng
 * @since 1.0 2020-11-02
 */
public class SimpleTokenReader implements TokenReader
{
	private final List<Token> tokenList;
	private int position = 0;

	public SimpleTokenReader(List<Token> tokenList)
	{
		this.tokenList = tokenList;
	}

	@Override
	public Token read()
	{
		if (position < tokenList.size())
		{
			return tokenList.get(position++);
		}
		return null;
	}

	@Override
	public Token peek()
	{
		if (position < tokenList.size())
		{
			return tokenList.get(position);
		}
		return null;
	}

	@Override
	public void unread()
	{
		if (position > 0)
		{
			position--;
		}
	}

	@Override
	public int position()
	{
		return position;
	}

	@Override
	public void setPosition(int position)
	{
		if (position >= 0 && position < tokenList.size())
		{
			this.position = position;
		}
	}

	@Override
	public List<Token> tokens()
	{
		return List.copyOf(tokenList);
	}
}
